package com.hunt.controller;

import com.hunt.model.app.FinancialDataInfo;
import com.hunt.service.app.FinancialDataService;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;


public class QuotationFlow {

    @Autowired
    private FinancialDataService financialDataService;

    @Test
    public void updateQuotationFlow(){
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        try {
            // 1.拿到一个httpclient的对象
            httpClient = HttpClients.createDefault();
            // 2.设置请求方式和请求信息
            HttpPost httpPost = new HttpPost("https://opensource.zealink.com/API/StockHistoryDay");
            //2.1 提交header头和参数
            httpPost.addHeader("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36");
            ArrayList<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>();
            parameters.add(new BasicNameValuePair("symbol[]", "600000.sh"));
            parameters.add(new BasicNameValuePair("symbol[]", "600004.sh"));

            parameters.add(new BasicNameValuePair("field[]", "capital.a"));
            httpPost.setEntity(new UrlEncodedFormEntity(parameters));

            // 3.执行请求
            response = httpClient.execute(httpPost);
            // 4.获取返回值
            String html = EntityUtils.toString(response.getEntity(), Charset.forName("utf-8"));
            // 5.打印
            System.out.println(html);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (httpClient != null){
                    httpClient.close();
                }
                if (response != null){
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
